#include "ModelIndexBuffer.h"

ModelIndexBuffer::ModelIndexBuffer(BYTE* indexData, UINT64 numIndices, UINT64 sizeBytes, UINT indexType)
{
	pIndexBuffer = NULL;

	this->indexData = new BYTE[(size_t)sizeBytes];
	memcpy(this->indexData, indexData, (size_t)sizeBytes);
	this->numIndices = numIndices;
	this->sizeBytes = sizeBytes;
	this->indexType = indexType;
}

ModelIndexBuffer::~ModelIndexBuffer(void)
{
	delete this->indexData;
	if (pIndexBuffer != 0) pIndexBuffer->Release();
	pIndexBuffer = NULL;
}

HRESULT ModelIndexBuffer::LoadGraphicsContent(EnginePointer<ID3D10Device> pDevice)
{
	D3D10_BUFFER_DESC bd;
	bd.Usage = D3D10_USAGE_DEFAULT;
	bd.ByteWidth = (UINT)sizeBytes;
	bd.BindFlags = D3D10_BIND_INDEX_BUFFER;
	bd.CPUAccessFlags = 0;
	bd.MiscFlags = 0;

	D3D10_SUBRESOURCE_DATA InitData;
	InitData.pSysMem = indexData;

	HRESULT hr = pDevice->CreateBuffer(&bd,& InitData,& pIndexBuffer);
	if (FAILED(hr)) return hr;

	return S_OK;
}

void ModelIndexBuffer::UnloadGraphicsContent()
{
	if (pIndexBuffer != 0) pIndexBuffer->Release();
	pIndexBuffer = 0;
}

ID3D10Buffer* ModelIndexBuffer::getBuffer()
{
	return pIndexBuffer;
}